package com.yidu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnProductionMapper;
import com.yidu.dao.snManufactureMapper;
import com.yidu.model.SnProDetail;
import com.yidu.model.SnProduction;
import com.yidu.model.snManufacture;
import com.yidu.service.SnManufactureService;

/**
 * 制造计划实现类
 * @author chensituo
 * @date 2018年12月10日 下午4:35:47
 */
@Service
public class SnManufactureServiceImpl implements SnManufactureService{
	/**
	 * 注入制造计划mapper
	 */
	@Resource
	snManufactureMapper manufactureMapper;
	
	/**
	 * 注入生产计划mapper
	 */
	@Resource
	SnProductionMapper productionMapper;
	

	@Override
	public int addOrUpdate(String deId, String comId, String number) {
		//调用根据生产详情id查询
		snManufacture manufacture=manufactureMapper.selectDeId(deId);
		int rows=0;
		if (manufacture==null) {
			//增加
			snManufacture manu=new snManufacture();
			manu.setSmId(Tools.getDateOrderNo());
			manu.setDeId(deId);
			manu.setSmNumber(Integer.valueOf(number));
			manu.setSmStocks(0);
			manu.setSmUndone(Integer.valueOf(number));
			manu.setSmState(0);
			manu.setIsva(1);
			manu.setOptime(Tools.getCurDateTime());
			manu.setOper("0");
			manu.setSort(Integer.valueOf(Tools.getTimeStamp()));
			//调用增加的方法
			rows=manufactureMapper.insertSelective(manu);
		}else {
			//修改
			manufacture.setSmNumber(Integer.valueOf(number));
			rows=manufactureMapper.updateByPrimaryKeySelective(manufacture);
		}
		return rows;
	}

	@Override
	public List<SnProDetail> findAll(Map<String, Object> maps) {
		return manufactureMapper.findAll(maps);
	}

	@Override
	public int findCount(Map<String, Object> maps) {
		// TODO Auto-generated method stub
		return manufactureMapper.findCount(maps);
	}

	@Override
	public List<snManufacture> query(String proId) {
		return manufactureMapper.query(proId);
	}

	@Override
	public int updateNumber(String sum,String proId) {
		int rows=0;
		//将字符串拆分
		String [] str=sum.split("@");
		//遍历
		for (String string : str) {
			//根据，号拆分
			String [] arr=string.split(",");
			//调用根据id查询的方法
			snManufacture manufacture=manufactureMapper.selectByPrimaryKey(arr[0]);
			//赋值
			manufacture.setSmStocks(manufacture.getSmStocks()+Integer.valueOf(arr[1]));
			
			manufacture.setSmUndone(manufacture.getSmNumber()-manufacture.getSmStocks());
			manufacture.setOper((Integer.valueOf(manufacture.getOper())+1)+"");
			//如果制造完毕
			System.out.println(manufacture.getSmNumber()+"****"+manufacture.getSmStocks()+"*****"+manufacture.getSmUndone());
			if((manufacture.getSmNumber()==manufacture.getSmStocks() || manufacture.getSmNumber().equals(manufacture.getSmStocks()))  && (manufacture.getSmUndone()==0 || manufacture.getSmUndone().equals(0))) {
				//将状态修改为已完成
				manufacture.setSmState(1);
			}
			//调用制造修改的方法
			rows=manufactureMapper.updateByPrimaryKeySelective(manufacture);
			
			if(rows==0) {
				break;
			}
		}
		//创建一个制造计划model
		snManufacture manufacture=new snManufacture();
		manufacture.setProId(proId);
		//创建一个map
		Map<String, Object> map=new HashMap<>();
		map.put("manufacture", manufacture);
		//得到总制造数
		int count=manufactureMapper.findCount(map);
		//钓鱼用查询已完成行数的方法
		int notcount=manufactureMapper.findCounts(proId);
		//判断制造计划是否全部完成
		if (count==notcount) {
			//调用根据id查询的方法
			SnProduction production=productionMapper.selectByPrimaryKey(proId);
			//将状态赋值
			production.setProState(3);
			//调用修改的方法
			return productionMapper.updateByPrimaryKeySelective(production);
		}
		
		return rows;
	}
}
